package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.news.NewsInfoSearch;
import com.ruoyi.system.domain.news.dto.NewsInfoSearchDto;
import com.ruoyi.system.domain.news.query.NewsInfoSearchQuery;
import com.ruoyi.system.domain.news.vo.NewsInfoSearchVo;
import com.ruoyi.system.service.news.NewsInfoSearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/newsInfoSearch")
@Api(value = "newsInfoSearchController", tags  = "信息查询")
public class NewsInfoSearchController extends BaseController {

	@Resource
	private NewsInfoSearchService newsInfoSearchService;

    @ApiOperation(value = "新增/修改信息查询信息", notes = "新增/修改信息查询信息")
	@PostMapping("/submit")
	public R submit(@RequestBody NewsInfoSearchDto newsInfoSearchDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		NewsInfoSearch newsInfoSearch = new NewsInfoSearch();
		BeanUtils.copyProperties(newsInfoSearchDto,newsInfoSearch);
		if (newsInfoSearch.getId()==null) {
			newsInfoSearch.setCreateBy(loginUser.getUsername());
			newsInfoSearch.setDeptId(loginUser.getDeptId());
			newsInfoSearch.setCreateTime(new Date());
			newsInfoSearch.setUpdateBy(loginUser.getUsername());
			newsInfoSearch.setUpdateTime(new Date());
		} else {
			newsInfoSearch.setUpdateBy(loginUser.getUsername());
			newsInfoSearch.setUpdateTime(new Date());
		}
		boolean result = newsInfoSearchService.saveOrUpdate(newsInfoSearch);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除信息查询信息", notes = "删除信息查询信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = newsInfoSearchService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个信息查询信息", notes = "获取单个信息查询信息")
	@GetMapping("/getById")
	public R<NewsInfoSearchVo> getById(@RequestParam(value = "id") Long id) {
		NewsInfoSearchQuery newsInfoSearchQuery = new NewsInfoSearchQuery();
		newsInfoSearchQuery.setId(id);
		List<NewsInfoSearchVo> newsInfoSearchList = newsInfoSearchService.getNewsInfoSearchList(newsInfoSearchQuery);
		if (newsInfoSearchList!=null&&newsInfoSearchList.size()>0) {
			return R.ok(newsInfoSearchList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取信息查询信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<NewsInfoSearchVo>> list(@RequestBody NewsInfoSearchQuery newsInfoSearchQuery) {
		List<NewsInfoSearchVo> newsInfoSearchList = newsInfoSearchService.getNewsInfoSearchList(newsInfoSearchQuery);
		return R.ok(new PageInfo<>(newsInfoSearchList));
	}
}
